/*
 * @Description  :
 * @Version      : 1.0
 * @Author       : Shiming
 * @Date         : 2021-12-14 15:02:52
 * @LastEditors  : Shiming
 * @LastEditTime : 2022-01-18 17:20:58
 * @FilePath     : \\tms-obc-web\\src\\app\\routes\\order-management\\modal\\bulk\\confir-receipt\\confir-receipt.component.ts
 * Copyright (C) 2022 huzhenhong. All rights reserved.
 */
import { Component, OnInit } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { NzUploadChangeParam, NzUploadFile } from 'ng-zorro-antd/upload';
import { Observable, Observer } from 'rxjs';
import {InterfaceDockingService} from "../../services/interface-docking.service";
import {SFSchema} from "@delon/form";
import { DatePipe } from '@angular/common';

function getBase64(file: File): Promise<string | ArrayBuffer | null> {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = error => reject(error);
  });
}
@Component({
  selector: 'waybill-confir-receipt',
  templateUrl: './confir-receipt.component.html',
  styleUrls: ['./confir-receipt.component.less'],
  providers: [DatePipe]
})
export class confirReceiptComponent implements OnInit {
  i: any;
  Status: any;
  data: any = {};
  schema!: SFSchema;
  listImagUrls: any[] = [];
  previewImage1 = '';
  previewVisible1 = false;
  inputValue: string = ''
  inputTime!: Date
  constructor(
    private modal: NzModalRef,
    private msgSrv: NzMessageService,
    public http: _HttpClient,
    public service: InterfaceDockingService,
    private datePipe: DatePipe
  ) {}

  ngOnInit(): void {
    console.log(this.i);
    console.log(this.i?.representationName);
    if(this.i) {
      this.inputValue = this.i?.representationName
      this.inputTime = this.i?.representationTime
      if(this.i?.representationFirstFilePath != null) {
        this.listImagUrls = [{
          uid: '-1',
          name: 'image.png',
          status: 'done',
          url: this.i?.representationFirstFilePath
        }]
      }
    }
  }

  save(): void {
    let imgList: any = [];
    if (this.listImagUrls.length > 0) {
      this.listImagUrls.forEach((res: any) => {
        if (res.url) {
          imgList.push(res.url);
        }
      });
    }
    if(this.i.status  != 12) {
      this.service.msgSrv.warning('当前状态不能点击上传操作');
      return;
    }
    if(this.inputValue == null || this.inputValue == '') {
      this.service.msgSrv.warning('签收人必须填写');
      return;
    }
    if(this.inputTime == null) {
      this.service.msgSrv.warning('签收时间必须填写');
      return;
    }
    if(this.listImagUrls.length <= 0) {
      this.service.msgSrv.warning('回单图片必须上传');
      return;
    }
    const params = {
      ...this.i,
      representationFirstFilePath: imgList[0],
      representationTime: this.datePipe.transform(this.inputTime, 'yyyy-MM-dd HH:mm:ss'),
      representationName: this.inputValue
    };
    console.log(params);
    this.service.request(this.service.$api_confirmReceipt, params).subscribe((res: any) => {
      if (res) {
        this.service.msgSrv.success('确认签收成功！');
        this.modal.destroy(true);
      } else {
        this.service.msgSrv.error('确认签收失败！');
      }
    });
  }

  close(): void {
    this.modal.destroy(true);
  }
  userAction() {}
  handlePreview1 = async (file: NzUploadFile) => {
    if (!file.url && !file.preview) {
      file.preview = await getBase64(file.originFileObj!);
    }
    this.previewImage1 = file.url || file.preview;
    this.previewVisible1 = true;
  };
  handleChange1(info: NzUploadChangeParam): void {
    switch (info.file.status) {
      case 'uploading':
        break;
      case 'done':
        let fileList = [...info.fileList];
        // 2. Read from response and show file link
        console.log(fileList);
        fileList = fileList.map((file: any) => {
          console.log(file);

          if (file.response) {
            file.url = file.response?.data?.fullFilePath;
          }
          return file;
        });
        console.log(this.listImagUrls);
        break;
      case 'error':
        this.service.msgSrv.error('网络错误');
        break;
    }
  }
  beforeUpload = (file: NzUploadFile, _fileList: NzUploadFile[]) => {
    return new Observable((observer: Observer<boolean>) => {
      const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/gif' || file.type === 'image/bmp';
      if (!isJpgOrPng) {
        this.service.msgSrv.warning('只能上传图片!');
        observer.complete();
        return;
      }
      // tslint:disable-next-line: no-non-null-assertion
      const isLt2M = file.size! / 1024 / 1024 < 3;
      if (!isLt2M) {
        this.service.msgSrv.warning('图片大小超过3兆!');
        observer.complete();
        return;
      }
      observer.next(isJpgOrPng && isLt2M);
      observer.complete();
    });
  };
}
